import BaseController from './base';

export default class UserController extends BaseController {
  public async login() {
    const { ctx, app } = this;
    const user = await ctx.service.user.login(ctx.request.body);
    if (user) {
      const token = app.jwt.sign(
        {
          id: user.Id.toString(),
          username: user.username,
        },
        app.config.jwt.secret,
      );
      this.ok(token);
    } else {
      this.error('用户名/密码错误');
    }
  }

  public async getInfo() {
    const { ctx } = this;
    const user = await ctx.service.user.getUserInfo(ctx.user.id);
    this.ok({
      id: user?.Id.toString(),
      userName: user?.username,
    });
  }
}
